using Godot;
using System;

public partial class ScreenTransition : CanvasLayer
{
	[Signal] 
	public delegate void TransitionedHalfwayEventHandler();
	bool skipEmit = false;


	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
	}


	public async void Transition(){
		GetNode<AnimationPlayer>("AnimationPlayer").Play("default");
		await ToSignal(this,nameof(TransitionedHalfway));
		GD.Print("Transition等待完成");
		skipEmit = true;
		//为什么不用PlayBackwards，因为动画中偶尔触发不了EmitTransitionedHalfway事件
		//EmitTransitionedHalfway事件放在动画结尾，偶尔触发不了EmitTransitionedHalfway事件
		//解决方案一  将时间往前推0.01秒，保证付放在最后一刻
		//解决方案二  新创建一个动画
		// GetNode<AnimationPlayer>("AnimationPlayer").PlayBackwards("default");
		GetNode<AnimationPlayer>("AnimationPlayer").Play("out");
	}

	public async void TransitionToScene(String scenePath){
		this.Transition();
		await ToSignal(this,nameof(TransitionedHalfway)); //等待信号结束 
		GetTree().ChangeSceneToFile(scenePath);
	}

	public void EmitTransitionedHalfway(){
		GD.Print("发送",skipEmit);
		if(skipEmit){
			skipEmit = false;
			return;
		}
		
		this.EmitSignal(nameof(TransitionedHalfway));
	}
}
